<!-- 角色编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="460"
    v-model="visible"
    :title="actionConfigs[action].title"
    @open="handleOpen"
    @close="handleCancel"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="currentRules"
      label-width="80px"
      @submit.prevent=""
    >
      <el-form-item label="角色名称" prop="role_name">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.role_name"
          placeholder="请输入角色名称"
        />
      </el-form-item>
      <el-form-item label="角色标识" prop="role_code">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.role_code"
          placeholder="请输入角色标识"
        />
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          :rows="4"
          type="textarea"
          v-model="form.comments"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit">
        {{ actionConfigs[action].button }}
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { addRole, updateRole, getRole } from "@/api/system/role";

import { useFormEdit } from "@/hooks/useFormEdit";
const props = defineProps({
  /** 修改回显的数据 */
  data: Object,
  action: {
    type: String,
    default: "edit",
    validator: (value) =>
      ["add", "edit", "audit", "push", "view"].includes(value),
  },
});

const emit = defineEmits(["update:modelValue", "done", "open"]);

const actionConfigs = {
  add: {
    title: "新增角色",
    button: "新增",
    api: addRole,
    showFields: ["role_name", "role_code", "comments"],
    disabledFields: [],
    rules: {
      role_code: [
        {
          required: true,
          message: "请输入用户账号",
          type: "string",
          trigger: "blur",
        },
      ],
      role_name: [
        {
          required: true,
          message: "请输入用户名",
          type: "string",
          trigger: "blur",
        },
      ],
    },
    beforeOpen: async ({ type, formData }) => {
      // 打开前的操作，可以修改表单数据
      console.log("打开前的操作");
      return { type, formData }; // 必须返回处理后的表单数据
    },
    beforeSubmit: async ({ type, formData }) => {
      // 提交前的操作，可以修改表单数据
      console.log("提交数据:", formData);
      // if (!formData.requiredField) {
      // return Promise.reject(new Error("缺少必填字段")); // 返回rejected Promise中断流程
      // }
      return { type, formData }; // 必须返回处理后的表单数据
    },
  },
  edit: {
    title: "修改角色",
    button: "保存",
    api: updateRole, // 补充edit的API
    showFields: ["role_name", "role_code", "comments"],
    disabledFields: [],
    rules: {
      role_code: [
        {
          required: true,
          message: "请输入用户账号",
          type: "string",
          trigger: "blur",
        },
      ],
      role_name: [
        {
          required: true,
          message: "请输入用户名",
          type: "string",
          trigger: "blur",
        },
      ],
    },
    beforeSubmit: async ({ type, formData }) => {
      // 提交前的操作，可以修改表单数据
      console.log("提交数据:", formData);
      // // 添加实际验证逻辑
      // if (
      //   !formData.dict_data_name ||
      //   !formData.dict_data_code ||
      //   !formData.sort_number
      // ) {
      // return Promise.reject(new Error("请填写所有必填字段"));
      // }
      return { type, formData }; // 必须返回处理后的表单数据
    },
  },
  view: {
    title: "查看字典数据",
    button: "确定",
    // api: showDictionaryData, // 补充view的API
    showFields: ["role_name", "role_code", "comments"],
    disabledFields: [],
    rules: {},
  },
};

const {
  form,
  formRef,
  visible,
  loading,
  handleOpen,
  handleSubmit,
  currentRules,
  handleCancel,
} = useFormEdit(props, emit, {
  actionConfigs,
  formFields: {
    role_id: void 0,
    role_name: "",
    role_code: "",
    comments: "",
  },
  detailApi: getRole,
  idField: "role_id",
});
</script>
